//master - Leonardo

#include <Wire.h>

int c,x,y,r,g,b;

void setup(){
    Serial.begin(19200);
    Wire.begin();
}
 
void loop(){
    while(Serial.available() > 0){
        c = Serial.parseInt(); //maji se zhasnout dosud svitici LED?
        x = Serial.parseInt();
        y = Serial.parseInt();
        r = Serial.parseInt();
        g = Serial.parseInt();
        b = Serial.parseInt();
        
        if(x >= 16 || y >= 16 || r >= 256 || g >= 256 || b >= 256){
            Serial.println("Chybna data, nebo neplatny rozsah.");    
        }   
        else{
            if(c == 1){
                clearDisplay(100); 
                clearDisplay(101);
                clearDisplay(102);
                clearDisplay(103);
            }
            if(x < 8 && y < 8){
                //levy spodni
                setPixel(100);
            }
            else if(x < 8 && y >= 8){
                //levy horni
                y = y%8;
                setPixel(101);
            }
            else if(x >= 8 && y < 8){
                //pravy spodni
                x = x%8;
                setPixel(102);
            }
            else if(x >= 8 && y >= 8){
                //pravy horni
                x = x%8;
                y = y%8;
                setPixel(103);
            }
        }
    }    
}
void clearDisplay(int addr){
    Wire.beginTransmission(addr);
    Wire.write(1); //pouze zhasne svitici LED
    Wire.write(0);
    Wire.write(0);
    Wire.write(0);
    Wire.write(0);
    Wire.write(0);
    Wire.endTransmission();
}

void setPixel(int addr){
    Wire.beginTransmission(addr);
    Wire.write(c);
    Wire.write(x);
    Wire.write(y);
    Wire.write(r);
    Wire.write(g);
    Wire.write(b);
    Wire.endTransmission();
}